package majorityElement;

public class Solution {
    public int majorityElement(int[] nums) {
        // 摩尔投票法
        int ans = nums[0],time = 0;
        for (int num : nums) {
            if( ans == num ) {
                time ++;
            } else {
                time --;
            }
            if( time == 0) {
                ans = num;
                time = 1;
            }
        }
        return ans;
    }
}
